package com.fanhao.dllo.justforyou.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.NewsCommentHotViewAdapter;
import com.fanhao.dllo.justforyou.adapter.NewsCommentNewViewAdapter;
import com.fanhao.dllo.justforyou.entity.HotPost;
import com.fanhao.dllo.justforyou.entity.NewPost;
import com.fanhao.dllo.justforyou.entity.NewsCommentNewPostsResult;
import com.fanhao.dllo.justforyou.entity.NewsViewPageInforResult;
import com.fanhao.dllo.justforyou.view.HotListView;
import com.fanhao.dllo.justforyou.view.NewListView;
import com.fanhao.dllo.justforyou.volley.StringRequestUtf;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class NewsViewpageInforCommentActivity extends AppCompatActivity implements View.OnClickListener {
    private HotListView hotView;
    private NewListView newView;
    private NewsCommentHotViewAdapter hotViewAdapter;
    private NewsCommentNewViewAdapter newViewAdapter;

    VolleySingleton volleySingleton = VolleySingleton.getInstance();
    
    private String id;
    private TextView hot_title_text,new_title_text;
    private ImageView backIv;
    private View hotHeaderView,newHeaderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_viewpage_infor_comment);
        
        getData();
        init();
        initData();
        initOther();
    }



    /***
 * 获取上一届面传来的postid
 * ***/
    public void getData() {
        Intent intent = getIntent();
        id = intent.getStringExtra("getPostid");

    }
    private void init() {
        hotView = (HotListView) findViewById(R.id.hotView);
        hotViewAdapter = new NewsCommentHotViewAdapter();


        newView = (NewListView) findViewById(R.id.newsView);
        newViewAdapter = new NewsCommentNewViewAdapter();

        // 加载表头布局
        hotHeaderView = getLayoutInflater().inflate(R.layout.hot_comment_title,null);
//        hot_title_text = (TextView)hotHeaderView.findViewById(R.id.hot_title_text);
        newHeaderView = getLayoutInflater().inflate(R.layout.new_comment_title,null);
//        new_title_text = (TextView)newHeaderView.findViewById(R.id.new_title_text);

        backIv = (ImageView)findViewById(R.id.backIv);
        backIv.setOnClickListener(this);

}



    private void initData() {
        String url = "http://comment.api.163.com/api/json/post/list/new/hot/photoview_bbs/" + id + "/0/10/10/2/2";

        JsonObjectRequest request = new JsonObjectRequest(JsonObjectRequest.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            if (response.has("hotPosts")){
                                JSONArray hotArray = response.getJSONArray("hotPosts");

                                List<HotPost> hotPosts = new ArrayList<>();
                                Gson gson = new Gson();

                                for (int i=0;i<hotArray.length();i++){
                                    JSONObject json = hotArray.getJSONObject(i);
                                    String s = json.getString("1");
                                    HotPost hotPost = gson.fromJson(s,HotPost.class);
                                    hotPosts.add(hotPost);

                                }

                                for (HotPost h:hotPosts){
                                    Log.i("lanou","---->>"+h.getF());
                                }

                                //如果有  则添加最热评论的表头
                                    hotView.addHeaderView(hotHeaderView);
                                hotViewAdapter.addData(hotPosts);
                                hotView.setAdapter(hotViewAdapter);


                            }


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        volleySingleton.addRequest(request);

    }

    private void initOther() {
        String url = "http://comment.api.163.com/api/json/post/list/new/normal/photoview_bbs/" +
                id +"/desc/0/20/10/2/2";

        JsonObjectRequest request = new JsonObjectRequest(JsonObjectRequest.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {

                        if (response.has("newPosts")) {
                            try {
                                JSONArray newArray= response.getJSONArray("newPosts");

                                List<NewPost> newPosts = new ArrayList<>();
                                Gson gson = new Gson();

                                for (int i = 0; i <newArray.length() ; i++) {
                                    JSONObject json = newArray.getJSONObject(i);
                                    String s = json.getString("1");

                                    NewPost newPost = gson.fromJson(s,NewPost.class);
                                    newPosts.add(newPost);

                                }
                                //如果有  则添加最新评论的表头
                                if (newPosts.size() > 0) {
                                    newView.addHeaderView(newHeaderView);
                                }
                                newViewAdapter.addData(newPosts);

                                newView.setAdapter(newViewAdapter);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        volleySingleton.addRequest(request);

    }

    @Override
    public void onClick(View v) {
        finish();
    }
}
